var xc,yc,interval,focusTip;
var submit_button;

function hideTip() {focusTip.hidetip();}
function showTip() {focusTip.display();}

function DocumentListener(elem) { // extends EventListener
  this.base = EventListener;
  this.base(elem);
  
  this.coords = {left:0, top:0};
  
  var me = this;
  
  this.init = function() {
    this.register('mousemove', this.setCoordinates);  
    //this.register('mousemove', this.debug);      
  }
  
  this.debug = function() {
    document.getElementById("debug").innerHTML = "(" + me.coords.left + ", " + me.coords.top + ")";  
  }
  
  this.setCoordinates = function(evt) {
    me.invoke(evt);
    var x, y;
    if (evt.pageX) {
      x = me.evt.pageX;
      y = me.evt.pageY;  
    }
    else if (evt.clientX) {
      var b = document.body;
      x = me.evt.clientX + b.scrollLeft - b.clientLeft;
      y = me.evt.clientY + b.scrollTop - b.clientTop;
      if (b.parentElement && b.parentElement.clientLeft) {
        p = b.parentElement;
        x += p.scrollLeft + p.clientLeft;
        y  += p.scrollTop + p.clientTop;  
      }
      y -= 4;      
    }
    me.coords.left = x;
    me.coords.top  = y;
  }  
}

function ToolTipListener(elem, tooltip, doc) { // extends EventListener
  this.base = EventListener;
  this.base(elem);
  this.tooltip = tooltip;
  this.doc = doc;
  this.tip;

  var me = this;
  
  this.init = function() {
    this.tip = new String(this.elem.getAttribute('title'));
    if (this.tip != "null") {
      this.register('mouseover', this.showtip); 
      this.register('mouseout',  this.hidetip);     
    }

    this.elem.setAttribute('title', '');  
  }
  
  this.showtip = function(evt) {
    clearInterval(interval);
    focusTip = me;
    interval = setTimeout("showTip()", 500);
  }

  this.hidetip = function(evt) {
    me.tooltip.style.display = "none";
    focusTip = "undefined";
    clearInterval(interval);    
  }
  
  this.display = function() {
 	  this.tooltip.style.top = (this.doc.coords.top+20) + "px";
 	  this.tooltip.style.left = this.doc.coords.left + "px";  
 	  this.tooltip.innerHTML = this.tip;  	 
    this.tooltip.style.display = "block";    
    interval = setTimeout("hideTip()", 3000);    
  }

}
ToolTipListener.prototype = new EventListener;

function __init()
{
  if (document.getElementsByTagName("form").item(0)) {
    var tooltip, doc, ancs, elem, listener, i;  
    
    tooltip = document.createElement("div");
    tooltip.id = "tip";
    document.getElementsByTagName("body").item(0).appendChild(tooltip);
    
    doc = new DocumentListener(document);
    doc.init();
  
    elems = document.getElementsByTagName("form").item(0).elements;  
    for (i = 0; elem = elems.item(i++); ) {
      if (elem.nodeName == "INPUT" || elem.nodeName == "TEXTAREA") {
        listener = new ToolTipListener(elem, tooltip, doc);
        listener.init();    
        if (elem.getAttribute("type") == "submit") {
          submit_button = listener;  
        }    
      }
    }
    elems = document.getElementsByTagName("label");
    for (i = 0; elem = elems.item(i++); ) {
      listener = new ToolTipListener(elem, tooltip, doc);
      listener.init();
    }  
  }
}

addLoadEvent(__init);